package com.ilop.sthome.ui.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.aliyun.iot.aep.sdk.login.ILoginCallback;
import com.aliyun.iot.aep.sdk.login.LoginBusiness;
import com.aliyun.iot.push.PushManager;
import com.example.common.base.BaseActivity;
import com.example.common.utils.SpUtil;
import com.ilop.sthome.utils.ProtocolFilterUtil;
import com.ilop.sthome.utils.greenDao.manage.DaoManager;
import com.ilop.sthome.utils.system.AppUtil;
import com.siterwell.familywellplus.R;

/**
 * @author skygge
 * @Date on 2019-09-25.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：启动页面
 */
public class StartActivity extends BaseActivity {

    private boolean isAgree;
    private LinearLayout mLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme);
        setContentView(R.layout.activity_start);
        initialization();
    }

    @Override
    protected void initialize() {
        super.initialize();
        DaoManager.getInstance().DBInitialize();
    }

    @Override
    protected void initView() {
        super.initView();
        mLayout = findViewById(R.id.layout_start);
        TextView mVersion = findViewById(R.id.tv_app_version);
        String versionName = AppUtil.getVersionName(mContext);
        String version = getString(R.string.app_version_hint) + "\t\t" + versionName;
        boolean isDebug = AppUtil.isApkInDebug(mContext);
        if (isDebug){
            String finalVersion = version+ "." + AppUtil.getVersionCode(mContext);
            mVersion.setText(finalVersion);
        }else {
            mVersion.setText(version);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        isAgree = SpUtil.getBoolean(mContext, "isAgree", false);
        if (!isAgree){
            hasPrivacyProtocol();
        }else {
            mHandler.postDelayed(this::doNext, 1500);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mLayout.setOnClickListener(view -> {
            if (!isAgree) hasPrivacyProtocol();
        });
    }

    /**
     * 需要同意隐私协议
     */
    private void hasPrivacyProtocol(){
        ProtocolFilterUtil mFilter = new ProtocolFilterUtil();
        mFilter.setAction(agree -> {
            if (agree){
                mHandler.post(this::doNext);
            }else {
                showToast(getString(R.string.rejected_agreement));
            }
        });
        mFilter.setContext(mContext);
    }

    /**
     * 去登陆/进入主页面
     */
    private void doNext(){
        if (LoginBusiness.isLogin()) {
            loginIn();
        } else {
            LoginBusiness.login(new ILoginCallback() {
                @Override
                public void onLoginSuccess() {
                    loginIn();
                }

                @Override
                public void onLoginFailed(int i, String s) {
                }
            });
        }
    }

    private void loginIn(){
        DaoManager.getInstance().init(this); //初始化GreenDao
        PushManager.getInstance().bindUser();
        startActivity(new Intent(mContext, MainActivity.class));
        finish();
    }
}
